<template>
  <div class="contaner">
    <div class="pack">
      <img src="@/assets/header.png" alt="">
    </div>
    <!-- <div class="header">
      <h2>金陵石化设备清册</h2>
    </div> -->
    <div class="login">
      <el-form 
        :model="loginForm"
        status-icon 
        :rules="rules" 
        ref="ruleForm" 
        label-width="100px" 
        class="demo-ruleForm"
      >
        <el-form-item label="账号" prop="username">
          <el-input type="text" v-model="loginForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="loginForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item class="center">
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  import { mapActions } from 'vuex'
  export default {
    data() {
      var validateUser = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入账号'));
        } else {
          callback();
        }
      };
      var validatePwd = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          callback();
        }
      };
      return {
        loginForm: {
          username: '',
          password: ''
        },
        rules: {
          username: [
            { validator: validateUser, trigger: 'blur' }
          ],
          password: [
            { validator: validatePwd, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      ...mapActions('LoginModule', ['toLogin']),
      async submitForm(formName) {
        try {
          // 表单验证
          const valid = await this.$refs[formName].validate();
          if (valid) {
            // 等待登录结果
            const isSuccess = await this.toLogin(this.loginForm);
            
            if (isSuccess) {
              // 登录成功后再跳转
              this.$router.push("/goods");
            }
          } else {
            this.$message.error("账号或密码不合法！");
            return false;
          }
        } catch (error) {
          console.error('登录错误:', error);
          this.$message.error("登录过程发生错误");
        }
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style lang="less" scoped>
  .contaner{
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background-color: #104468;
    .pack{
      width: 100px;
      height: 100px;
      background-color: #fff;
      position: absolute;
      left: 50%;
      top: 140px;
      margin-left: -60px;
      z-index: 2;
      border-radius: 50%;
      overflow: hidden;
      border: 10px solid #F5F5F5;
      img{
        width: 100px;
        height: 100px;
      }
    }
    .header{
    margin-bottom: 20px; /* 添加适当的间隙 */
    h2{
      font-size: 2em; /* H2大字号 */
      margin: 0; /* 去掉默认的外边距 */
    }
  }
    .login{
      width: 400px;
      height: 260px;
      padding: 20px;
      background-color: #fff;
      border-radius: 5px;
      position: absolute;
      top: 50%;
      left: 50%;
      margin-left: -225px;
      margin-top: -155px;
      .demo-ruleForm{
        position: relative;
        margin-left: -60px;
        margin-top: 50px;
        .center{
          text-align: center;
          margin-left: -20px;
          button{
            margin: 10px;
          }
        }
      }
    }
  }
</style>